using DbaseInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace dbinfo
{
    public partial class dbinfo : Form
    {
        private SelectDatabase _selected_database = null;
        private DatabaseInfo _dbase_info;
        private TableInfo _selected_table;

        public dbinfo()
        {
            InitializeComponent();

            // Get database details ...
            _selected_database = new SelectDatabase();
            _selected_database.ShowDialog();

            // Got kosher connection ?
            if (_selected_database.IsValidConnection)
            {
                // Retrieve info for selected database
                _dbase_info = new DatabaseInfo(_selected_database.ConnectionString);

                // Populate the 'Tables' list box
                tablesListBox.DataSource = _dbase_info.Tables;
                tablesListBox.DisplayMember = "Name";
            }
        }

        /// <summary>
        /// Handler for 'Tables' list box selected item changed event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tablesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            TableInfo[] tables;

            try
            {
                // Make a note of the selected table
                tables = (TableInfo[])tablesListBox.DataSource;
                _selected_table = tables[tablesListBox.SelectedIndex];

                // Update the 'Fields' list box with selected table details
                fieldsListBox.DataSource = _selected_table.Fields;
                fieldsListBox.DisplayMember = "Combined";

                Console.WriteLine("Selected table - " + _selected_table.Name);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}